package com.example.neusoft.yuejian_tea;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends AppCompatActivity {

    private ViewPager vp;
    private List<ImageView>  list;
    private TextView tvgo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        tvgo=findViewById(R.id.textView3);
        tvgo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(GuideActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        tvgo.setVisibility(View.GONE);

        vp=findViewById(R.id.viewpager);
        list=new ArrayList<ImageView>();
        initViewPager();

    }

    public void initViewPager() {
        ImageView iv1 = new ImageView(this);
        iv1.setImageResource(R.drawable.guide0);
        ImageView iv2 = new ImageView(this);
        iv2.setImageResource(R.drawable.guide1);
        ImageView iv3 = new ImageView(this);
        iv3.setImageResource(R.drawable.guide2);
        list.add(iv1);
        list.add(iv2);
        list.add(iv3);

        vp.setAdapter(new MyPagerAdapter());
        //监听ViewPager滑动效果
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            //页卡被选中的方法
            @Override
            public void onPageSelected(int arg0) {
                //如果是第三个页面
                if (arg0 == 2) {
                    tvgo.setVisibility(View.VISIBLE);
                } else {
                    tvgo.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    class MyPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        //初始化item实例方法
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }

        //item销毁的方法
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(list.get(position));
        }
    }

/*---------------------
    作者：陈李冠
    来源：CSDN
    原文：https://blog.csdn.net/chenliguan/article/details/49622397?utm_source=copy
    版权声明：本文为博主原创文章，转载请附上博文链接！*/
}
